home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12472 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: abacus.abasoft.co.uk!not-for-mail
  2. From: dmb@abacus.abasoft.co.uk (David Byrne)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ problem with constructor.
  5. Date: 19 Mar 1996 15:13:34 -0000
  6. Organization: Abacus Software Ltd.
  7. Message-ID: <4imiuu$k3p@abacus.abasoft.co.uk>
  8. References: <DoGGGp.Muy@latcs1.lat.oz.au>
  9. NNTP-Posting-Host: abacus.abasoft.co.uk
  10. X-NNTP-Posting-Host: abacus.demon.co.uk
  11.  
  12. In article <DoGGGp.Muy@latcs1.lat.oz.au>,
  13. Gregary J Boyles <boylesgj@lion.cs.latrobe.edu.au> wrote:
  14. >
  15. >int main()
  16. >{
  17. >     Address Address1(56,"Derby Drive","Epping",3165);
  18. >     Address Address2(22,"Claremont Street","Fawkner",3060);
  19. >
  20. >     /********************************************************
  21. >      After the above two calls Address1 and Address2 contain
  22. >      all the appropriate data however after the next call all
  23. >      3 contain 0 or null strings. WHY?
  24. >      ********************************************************/
  25.  
  26. It's only by an act of God that it doesn't blow up sooner ! Your problem
  27. is that Address.Street and Address.City are uninitialised char*
  28. When Address::Address(...) is called, the (random) memory areas which are
  29. pointed to by these data members are trashed. See below.
  30. >
  31. >     Address Address3(Address1);
  32. >     return(0);
  33. >}
  34.  
  35. >Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  36. >{
  37. >     Number=ANumber;
  38. >     strcpy(Street,AStreet);
  39. >     strcpy(City,ACity);
  40.  
  41. You should do something
  42. like:
  43.  
  44.     Street = new char[30];
  45.     strcpy(Street, AStreet);
  46.     City = new char[30];
  47.     strcpy(City, ACity);
  48.  
  49. and same in Address::Address()
  50.  
  51. >     Zip=AZip;
  52. >}
  53.  
  54. [snip]
  55.  
  56. Regards,
  57.  
  58. David
  59. -- 
  60. David Byrne, Abacus Software, London, UK              Tel: +44 (0)171 603 9877
  61. Email: dmb@abacus.demon.co.uk                         Fax: +44 (0)171 603 6844
  62. Here's a koan: If you have ice-cream I will give you some. If you have none,
  63.                I will take it away from you. (it's an ice-cream koan).
  64.